<?php
if(isset($_POST['userID'])){
    $db = new Database();
    $points = $_POST['Points'];
    $type = stripcslashes($_POST['Name']);
    $id = $_POST['userID'];
    $dealer = $db->query("SELECT * FROM tbdealer WHERE dealerCode='$id'");
    foreach($dealer as $info)
    {
	$dateExpired = $info['expiryDate'];
    }
    //$getProduct = $db->query("SELECT * FROM tbproduct WHERE points <= '$points' AND type='$type' AND date > '$dateExpired' ");
    $getProduct = $db->query("SELECT * FROM tbproduct WHERE points <= '$points' AND type='$type' ");
    $a=0;
    foreach($getProduct as $b){
	$a++;
    }
?>
	<div class="body_content">
            <div class="position">
		
		<div class="label_header_position">
		<?php
		if(@$_POST['forSelect']==1){ //this is for report purposes
		}else{
		?>
		    <label class="h3">
			<b>		
			    <div style="float: left;margin-right: 10px;margin-top: 15px;">
			    <?php
			    
				echo $info['firstName']. " " . $info['lastName'];
			    
			    ?>    
			    </div>
			    <p style="float: left;margin-right: 10px;margin-top: 15px;">
			    -|-
			    </p>
			    <div style="float: left;margin-right: 10px;margin-top: 15px;">
			    <?php
			    //$pointsType = $_POST['Name'];
			    $getProductLines = $db->query("SELECT * FROM tbproduct_line WHERE code='$type' AND is_active='1'","array");
                            foreach($getProductLines as $productLine){
                                $code = $productLine['code'];
                                $description = $productLine['description'];
                            }
                            if($code==$type){
                                echo $description;
                            }else{
				echo $type;
			    }
			    ?>
			    </div>
			    <p style="float: left;margin-right: 10px;margin-top: 15px;">
			    -|-
			    </p>
			    <div style="float: left;margin-right: 10px;margin-top: 15px;">
			    <?php echo "<span id='dealerSubPoints'>".$_POST['Points']."<span>". " " . "Points"; ?>
			    </div>
			    <p style="float: left;margin-right: 10px;margin-top: 15px;">
			    -|-
			    </p>
			    <div style="float: left;margin-left: 10px;margin-top: 15px;">
			    <?php echo date('M d, Y'); ?>
			    </div>
                            
			</b>
                            <div style="float: right;margin-left: 10px;">
                                <input type="button" class="button BactToTop" value="Back to Dealer Points" />
                            </div>
		    </label>
		    <?php
		}
		?>
		</div>
		
		<div class="right align_right">
                    
		</div>
		<div class="hr"></div>
		<?php
		if(@$_POST['forSelect']==1){ //this is for report purposes
		    ?>
		    <div class="align_right">
		    <input type="button" id="hideDR" class="reports hide" title="Hide Dealer Transaction" value="Hide">
		    </div>
		    <?php
		}
		?>
		<?php
		if(@$_POST['forSelect']==1){
		    ?>
		    <div style="float: right" class="dateFilter">
			Start Date:<input type="text" id="startD">
			End Date:<input type="text" id="endD">
			<input type="button" class="filterD reports" value="Filter Date" rel="<?php echo $_POST['userID']; ?>" title="Search Transaction by Date">
		    </div>
		    <?php
		}
		?>
		
		<div class="content">
		    <?php
		    if(@$_POST['forSelect']==1){ //for report
			?>
			<div style="margin: 15px;" class="h4"><b>Dealer Transactions</b></div>
			<?php
		    }
		    ?>
			<input type="hidden" rel="<?php echo $_POST['Points']; ?>" class="dealerpoints" value="<?php echo $_POST['Points']; ?>">
			<table border="0" id="table_accomodation" class="table_accomodation">
			    <thead>
				<tr>
				    <th>Products Redeemed</th>
				    <th>Item Code</th>
				    <th>Points</th>
				    <th>Quantity</th>
				    <th>Total Points</th>
				</tr>
			    </thead>
			    <tbody>
				<div id="load_redeem"></div>
				<?php
				//$b=1;
				$ID = $_POST['userID'];
                                $db = new Database();
                                $points = $_POST['Points'];
                                //$type = $_POST['Name'];
				if(@$_POST['forSelect']==1){ //this is for report purposes
				    if(isset($_POST['filter'])){ //for filtering
					$startD = $_POST['startD'];
					$endD = $_POST['endD'];
					$getLastTransaction = $db->query("SELECT * FROM tbredeem WHERE dealerCode = '$ID' AND date >= '$startD' AND date <= '$endD'","array");
					$countRow = $db->query("SELECT * FROM tbredeem WHERE dealerCode = '$ID' AND date >= '$startD' AND date <= '$endD'","numrows");
				    }else{
					$getLastTransaction = $db->query("SELECT * FROM tbredeem WHERE dealerCode='$ID'","array");
					$countRow = $db->query("SELECT * FROM tbredeem WHERE dealerCode='$ID'","numrows");
				    }
				}else{
				    $getLastTransaction = $db->query("SELECT * FROM tbredeem WHERE dealerCode='$ID' and product_type = '$type'","array");
				    $countRow = $db->query("SELECT * FROM tbredeem WHERE dealerCode='$ID' and product_type = '$type'","numrows");
				}
				if($countRow>0){
				    foreach($getLastTransaction as $row){
					$red_id =  $row['product_id'];
					$quantity =  $row['quantity'];
					$product_type = $row['product_type'];
					//$getProd = $db->query("SELECT * FROM tbproduct where id = ");
					$prodID_explode = explode(",",$red_id);
					$quantity_explode = explode(",",$quantity);
					//Get Sub header decription from the database
					$getProductLines = $db->query("SELECT * FROM tbproduct_line WHERE code='$product_type' AND is_active='1'","array");
					foreach($getProductLines as $productLine){
					    $code = $productLine['code'];
					    $description = $productLine['description'];
					}
					if($code==$product_type){
					    echo "<tr  style='height: 30px;font-size: 20px;'><td colspan='1'><b>Transaction $description</b></td><td colspan='4'><center>Date: &nbsp&nbsp".$row['date']."</center></td></tr>";
					}else{
					    echo "<tr  style='height: 30px;font-size: 20px;'><td colspan='1'><b>Transaction $product_type</b></td><td colspan='4'><center>Date: &nbsp&nbsp".$row['date']."</center></td></tr>";
					}
					
					for($a=0;$a<count($quantity_explode);$a++){
					    $query = $db->query("SELECT * FROM tbproduct where id='$prodID_explode[$a]'","row");
					    //$data = mysql_fetch_array($query);
					    echo "<tr><td>".$query['product_name']."</td>
					    <td>".$query['itemCode']."</td>
					    <td>".$query['points']."</td>
					    <td>".$quantity_explode[$a]."</td>
					    <td>".$quantity_explode[$a]*$query['points']."</td>"."</tr> ";
					    $totalRedeemedPoints +=($quantity_explode[$a]*$query['points']);
					}
					echo "<tr><td colspan=5><center>Total Redeem Points: ".$totalRedeemedPoints."<br>Remaining Points: ".$row['remaining_points']."</center></td></tr>";
					//$b++;
					$totalRedeemedPoints = 0;
				    }
				}else{
				    echo "<tr  style='height: 30px;font-size: 20px;'><td colspan='5'><center>No Record Found!</center></td></tr>";
				}
				?>
			    </tbody>
			</table>
		</div>
            </div>
        </div>
	<script>
	    //datepicker function
	    $(function()
	    {
		    MakeStartEndPicker("#startD", "#endD");
	    });
	</script>
<?php
}
?>